package com.maserb.monitor.web.schoolNotice;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.SysCode;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolNotice.SchoolNoticeEntity;
import com.maserb.monitor.enums.SchoolNoticeTypeEnum;
import com.maserb.monitor.repository.schoolNotice.SchoolNoticeHistoryRepository;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolNotice.SchoolNoticeService;
import com.maserb.monitor.service.sysCode.SysCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通知通告后台管理
 */
@Controller
@RequestMapping("/schoolNoticeBack")
public class SchoolNoticeBackController {


    @Autowired
    SchoolNoticeService schoolNoticeService;

    @Autowired
    IToolService toolService;

    @Autowired
    JwOrgService orgService;

    @Autowired
    private SysCodeService codeService;




    @RequestMapping(value = {"/", "/index"})
    public ModelAndView getIndex(final HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity) session.getAttribute("user");
        List<JwOrgEntity> clazzList = new ArrayList<JwOrgEntity>();
        String userId = "";
        if(null!=user && !"".equals(user.getId())){
            userId = user.getId();
            JwOrgEntity org = orgService.findOne(user.getOrgId());
            clazzList = orgService.findByUser(user);
            clazzList.add(org);
        }
        view.setViewName("schoolNotice/index");
        SchoolNoticeTypeEnum[] noticeTypeEnums = SchoolNoticeTypeEnum.values();
        view.addObject("noticeTypeEnums",noticeTypeEnums);
        view.addObject("clazzList",clazzList);
        view.addObject("userId",userId);
        return view;
    }
    /**
     * 通知列表
     * @param type
     * @param keyword
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageResponseResult<SchoolNoticeEntity> doListSchoolStudent(PageRequestParam page,String userId,String clazzId, final String type, final String keyword,final String startTime,final String endTime) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<SchoolNoticeEntity>>() {
            @Override
            public Page<SchoolNoticeEntity> invoke(PageRequest var1) throws Exception {
                Page<SchoolNoticeEntity> page = schoolNoticeService.findAll(var1, userId, clazzId,type,keyword,startTime,endTime);
                for(SchoolNoticeEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getType() && !"".equals(entity.getType())){
                        entity.setType(SchoolNoticeTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/getAddSchoolNotice")
    public ModelAndView getAddSchoolNotice() {
        ModelAndView result = new ModelAndView();
        SchoolNoticeTypeEnum[] type = SchoolNoticeTypeEnum.values();
        List<SysCode>codeList = codeService.findByParent("SCHOOL_COURSE");
        result.addObject("codeList",codeList);
        result.addObject("type",type);
        result.setViewName("schoolNotice/add");
        return result;
    }

    @RequestMapping("/doAddSchoolNotice")
    @ResponseBody
    public AjaxResult<Boolean> doAddSchoolNotice(final HttpServletRequest request, SchoolNoticeEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setCreateTime(new Date());
                HttpSession session = request.getSession(true);
                UserEntity userEntity = (UserEntity) session.getAttribute("user");
                entity.setCreateUser(userEntity.getId());
                entity.setIsRead("0");
                schoolNoticeService.save(entity);
                //是否已读
                schoolNoticeService.addNoticeHistory(entity);
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateNotice/{id}")
    public ModelAndView getUpdateNotice(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        SchoolNoticeEntity entity = schoolNoticeService.findOne(id);
        entity.getOrg().setParent(null);
        entity.getOrg().setOrgChild(null);
        result.setViewName("schoolNotice/edit");
        List<SysCode>codeList = codeService.findByParent("SCHOOL_COURSE");
        result.addObject("codeList",codeList);
        SchoolNoticeTypeEnum[] type = SchoolNoticeTypeEnum.values();
        result.addObject("type",type);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateNotice")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateNotice(final HttpServletRequest request, SchoolNoticeEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                SchoolNoticeEntity SchoolNoticeEntity = schoolNoticeService.findOne(entity.getId());
                SchoolNoticeEntity.setTitle(entity.getTitle());
                SchoolNoticeEntity.setContent(entity.getContent());
                SchoolNoticeEntity.setType(entity.getType());
                SchoolNoticeEntity.setOrg(entity.getOrg());
                SchoolNoticeEntity.setUpdateTime(new Date());
                SchoolNoticeEntity.setSubjectId(entity.getSubjectId());
                schoolNoticeService.save(SchoolNoticeEntity);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteNotice")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteNotice(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                schoolNoticeService.deleteAllByKeys(keys);
                return true;
            }
        });
    }
}
